<template>
  <a-modal
    title="编辑概念属性"
    :width="modalWidth()"
    :centered="true"
    class="update-modal"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item
          label="属性名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入属性名称" v-decorator="['chName', {rules: [{required: true, message: '请输入属性名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="英文名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入英文名" v-decorator="['enName', {rules: [{required: true, message: '请输入英文名！'}]}]" />
        </a-form-item>
        <a-form-item
          label="数据类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            style="width: 100%"
            placeholder="请选择数据类型"
            v-decorator="['datetype', {rules: [{ required: true, message: '请选择数据类型！' }]}]">
            <a-select-option v-for="(item,index) in datetypeData" :key="index" :value="item.code">{{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="所属概念"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select
            style="width: 100%"
            placeholder="请选择所属概念"
            v-decorator="['schemaId', {rules: [{ required: true, message: '请选所属概念！' }]}]">
            <a-select-option v-for="(item,index) in schemaListData" :key="index" :value="item.id">{{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { propertyEdit } from '@/api/modular/main/knowl/property/propertyManage'
import { schemaTree, schemaList } from '@/api/modular/main/knowl/schema/schemaManage'
import { register } from '@/utils/drag_modal'

export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 }
      },
      datetypeData: [],
      queryParam: {},
      schemaId: undefined,
      schemaTreeData: [],
      schemaListData: [],
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    modalWidth() {
      return document.body.clientWidth * 0.5
    },
    // 初始化方法
    edit(record) {
      this.visible = true
      // this.getSchemaTree()
      this.getSchemaList()
      const datetypeOption = this.$options
      this.schemaId = record.schemaId
      this.datetypeData = datetypeOption.filters['dictData']('data_type')
      setTimeout(() => {
        this.form.setFieldsValue(
          {
            id: record.id,
            chName: record.chName,
            enName: record.enName,
            datetype: record.datetype
          }
        )
      }, 150)
      register(true)
    },
    getSchemaTree() {
      schemaTree(Object.assign(this.queryParam)).then((res) => {
        this.schemaTreeData = res.data
      })
    },
    getSchemaList() {
      schemaList(Object.assign(this.queryParam)).then((res) => {
        this.schemaListData = res.data
        this.form.setFieldsValue(
          {
            schemaId: this.schemaId
          }
        )
      })
    },
    initrParentSchema(value) {
      this.form.getFieldDecorator('schemaId', { initialValue: this.schemaListData.find(item => value === item.id).name })
    },
    handleSubmit() {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof (values[key]) === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          propertyEdit(values).then((res) => {
            if (res.success) {
              this.$message.success('编辑成功')
              this.confirmLoading = false
              this.$emit('ok', values)
              this.handleCancel()
            } else {
              this.$message.error('编辑失败')//  + res.message
            }
          }).finally((res) => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      register(false)
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
